<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getRoles, deleteRole } from "@/api/role";
import { ElMessage, ElMessageBox } from "element-plus";

const roles = ref<any[]>([]);
const loading = ref(false);
const pagination = ref({
  page: 1,
  page_size: 10,
  total: 0,
  total_page: 0
});

const fetchRoles = async () => {
  loading.value = true;
  try {
    const res = await getRoles({
      page: pagination.value.page,
      page_size: pagination.value.page_size
    });
    if (res.success) {
      roles.value = res.data;
      pagination.value = res.pagination;
    } else {
      ElMessage.error(res.message || "获取角色列表失败");
    }
  } catch (error) {
    ElMessage.error("获取角色列表失败");
  } finally {
    loading.value = false;
  }
};

const handleDelete = async (id: number) => {
  ElMessageBox.confirm("确定要删除该角色吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    try {
      const res = await deleteRole(id);
      if (res.success) {
        ElMessage.success("删除成功");
        fetchRoles();
      } else {
        ElMessage.error(res.message || "删除失败");
      }
    } catch (error) {
      ElMessage.error("删除失败");
    }
  }).catch(() => {
    // 取消删除
  });
};

const handlePageChange = (page: number) => {
  pagination.value.page = page;
  fetchRoles();
};

const handlePageSizeChange = (pageSize: number) => {
  pagination.value.page_size = pageSize;
  pagination.value.page = 1;
  fetchRoles();
};

onMounted(() => {
  fetchRoles();
});
</script>

<template>
  <div class="role-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>角色列表</span>
          <el-button type="primary" @click="$router.push('/role/create')">新增角色</el-button>
        </div>
      </template>
      
      <el-table :data="roles" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="$router.push(`/role/edit/${scope.row.id}`)">
              编辑
            </el-button>
            <el-button link type="danger" size="small" @click="handleDelete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.page_size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handlePageChange"
          @size-change="handlePageSizeChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>